﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class PHUONGDAO
    {
        public static bool ThemPHUONG(PHUONGDTO d)
        {
            DataProvider provider = new DataProvider();
            // Xay dung query
            String query = "Insert into PHUONG(MaPHUONG,TenPHUONG) values("
                         + "'" + d.MAPHUONG + "'"
                + "'" + d.TENPHUONG + "'"
                         + ")";
            return provider.ExecuteNonQuery(query);
        }
        public static List<PHUONGDTO> LayDanhSachPHUONG()
        {
            List<PHUONGDTO> lst = new List<PHUONGDTO>();

            // Xay dung query
            String query = "Select * from PHUONG";
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow row = dt.Rows[i];
                    PHUONGDTO nv = new PHUONGDTO();
                    nv.MAPHUONG = Convert.ToInt32(row["MaPhuong"]);
                    nv.TENPHUONG = Convert.ToString(row["TenPhuong"]);
                    // them vao danh sach
                    lst.Add(nv);
                }
            }

            return lst;
        }
        public static bool CapNhatPHUONG(PHUONGDTO nv)
        {
            DataProvider provider = new DataProvider();
            // Xay dung query
            String query = "Update PHUONG set "
                         + "TENPHUONG = " + "'" + nv.TENPHUONG + "'"
                         + " where "
                         + " ID = " + nv.MAPHUONG;
            return provider.ExecuteNonQuery(query);
        }
        public static bool XoaPHUONG(int id)
        {
            DataProvider provider = new DataProvider();
            // Xay dung query
            String query = "Delete from PHUONG where MAPHUONG = " + id;
            return provider.ExecuteNonQuery(query);
        }
    }
}